package org.cyz.tools.common.util;

import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.cyz.tools.common.dto.ApplicationInfoDTO;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

/**
 * @author abccy
 * @title: PomUtil
 * @projectName RedisGUI
 * @description: TODO
 * @date 2020-9-1515:11
 */
public class PomUtil {
    public static String getVersion(){
        try {
            //查看jar包里面pom.properties版本号
            String jarPath = PomUtil.class.getProtectionDomain().getCodeSource().getLocation().getFile();
            jarPath = java.net.URLDecoder.decode(jarPath, "UTF-8");
            try {
                URL url = new URL("jar:file:" + jarPath + "!/META-INF/maven/org.cyz.tools/RedisGUI/pom.properties");
                InputStream inputStream = url.openStream();
                Properties properties = new Properties();
                properties.load(inputStream);
                String version = properties.getProperty("version");
                return version;
            } catch (Exception e) {
                //开发过程中查看pom.xml版本号
                MavenXpp3Reader reader = new MavenXpp3Reader();
                String basePath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
                if (StringUtil.isWindows() && basePath.startsWith("/")) {
                    basePath = basePath.substring(1);
                }
                if (basePath.indexOf("/target/") != -1) {
                    basePath = basePath.substring(0, basePath.indexOf("/target/"));
                }
                Model model = reader.read(new FileReader(new File(basePath + File.separator+"pom.xml")));
                return model.getVersion();
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static ApplicationInfoDTO applicationInfo() throws IOException, XmlPullParserException {
        ApplicationInfoDTO applicationInfoDTO=new ApplicationInfoDTO();
        //查看jar包里面pom.properties版本号
        String jarPath = PomUtil.class.getProtectionDomain().getCodeSource().getLocation().getFile();
        jarPath = java.net.URLDecoder.decode(jarPath, "UTF-8");
        try {
            URL url = new URL("jar:file:" + jarPath + "!/META-INF/maven/org.cyz.tools/RedisGUI/pom.properties");
            InputStream inputStream = url.openStream();
            Properties properties = new Properties();
            properties.load(inputStream);
            String artifactId = properties.getProperty("artifactId");
            applicationInfoDTO.setArtifactId(artifactId);
            String groupId = properties.getProperty("groupId");
            applicationInfoDTO.setGroupId(groupId);
            String version = properties.getProperty("version");
            applicationInfoDTO.setVersion(version);
            return applicationInfoDTO;
        } catch (Exception e) {
            //开发过程中查看pom.xml版本号
            MavenXpp3Reader reader = new MavenXpp3Reader();
            String basePath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
            if (StringUtil.isWindows() && basePath.startsWith("/")) {
                basePath = basePath.substring(1);
            }
            if (basePath.indexOf("/target/") != -1) {
                basePath = basePath.substring(0, basePath.indexOf("/target/"));
            }
            Model model = reader.read(new FileReader(new File(basePath + File.separator+"pom.xml")));
            applicationInfoDTO.setArtifactId(model.getArtifactId());
            applicationInfoDTO.setGroupId(model.getGroupId());
            applicationInfoDTO.setVersion(model.getVersion());
            return applicationInfoDTO;
        }
    }


}
